package com.hssoftvn.android.apps.callmessmoneycontroller.util;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class GiXMLParser extends DefaultHandler
{

	boolean currentElement = false;
	
	boolean addingRes = false;
	boolean addingCell = false;
	boolean addingError = false;
	
	String currentValue = null;
	
	OpenCellIDResponse mResponse = null;
	
	public OpenCellIDResponse getResponse() {
		return mResponse;
	}

	//Used to check element with its attribute
	@Override
	public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException 
	{
		if (currentElement)
			currentValue = "";
		currentElement = true;
		
		if (localName.equals("rsp"))
		{
			mResponse = new OpenCellIDResponse();
		}
		else if (localName.equals("err"))
		{
			String val = attributes.getValue("info");
			mResponse.setErrorInfo(val);
			val = attributes.getValue("code");
			mResponse.setErrorCode(Integer.parseInt(val));
		}
		else if (localName.equals("cell"))
		{
			String val = attributes.getValue("lat");
			mResponse.setLat(Double.parseDouble(val));
			val = attributes.getValue("lon");
			mResponse.setLon(Double.parseDouble(val));
			val = attributes.getValue("mcc");
			mResponse.setMcc(Integer.parseInt(val));
			val = attributes.getValue("mnc");
			mResponse.setMnc(Integer.parseInt(val));
			val = attributes.getValue("lac");
			mResponse.setLac(Integer.parseInt(val));
			val = attributes.getValue("cellid");
			mResponse.setCellid(Integer.parseInt(val));
			val = attributes.getValue("averageSignalStrength");
			mResponse.setAverageSignalStrength(Integer.parseInt(val));
			val = attributes.getValue("range");
			mResponse.setRange(Integer.parseInt(val));
			val = attributes.getValue("samples");
			mResponse.setSamples(Integer.parseInt(val));
			val = attributes.getValue("changable");
			mResponse.setChangable(Integer.parseInt(val));
			val = attributes.getValue("radio");
			mResponse.setRadio(val);
			val = attributes.getValue("sid");
			mResponse.setSid(Integer.parseInt(val));
			val = attributes.getValue("nid");
			mResponse.setNid(Integer.parseInt(val));
			val = attributes.getValue("bid");
			mResponse.setBid(Integer.parseInt(val));
			
		} 
		else if (localName.equals("lat"))
		{
			String val = attributes.getValue("lat");
			mResponse.setLat(Double.parseDouble(val));
		}
	}

	//Used for element with data inside
	@Override
	public void endElement (String uri, String localName, String qName) 	throws SAXException 
	{
		currentElement = false;
		currentValue = currentValue.trim();
		currentValue = "";
	}

	//currentValue is the data of a tag 
	@Override
	public void characters (char[] ch, int start, int length) throws SAXException 
	{
		if (currentElement)
		{
			String value = new String(ch, start, length);
			if (value.equals("\n"))
				value = "";
			currentValue += value;
		}
	}
}
